let app = getApp();

Component({
  properties: {
    users: {  // 用户信息
      type: Object,
      value: [],
      observer: function(newVal, oldVal, changedPath) {
        this.setData({ user: newVal });
      },
    },
    openId: {  // openid
      type: String,
      value: '',
      observer: function(newVal, oldVal, changedPath) {
        this.setData({ openid: newVal });
      },
    },
    moduleName: {  // 模块名称
      type: String,
      value: '',
    },
  },

  data: {
    recharge: true,
    money: '',
    openid: '',
  },
  
  methods: {
    _recharge() {
      let that = this;
      let money = that.data.money;
      this.setData({
        recharge: true,
      });
      if (Number(money) < 0.01) {
        wx.showToast({
          title: '最小充值金额是0.01元',
          icon: 'none',
          duration: 1500,
          mask: true,
        });
        this.setData({
          recharge: false,
        });
        return;
      }
      if (isNaN(Number(money)) || money <= 0) return;
  
      // 调用会员卡储值接口
      that._getPayment(money);
  
      setTimeout(function() {
        that.setData({
          recharge: false,
        });
      }, 3500);
    },
  
    _bindKeyInput(e) {
      let that = this;
      let money = e.detail.value;
      that.setData({
        recharge: isNaN(Number(money)) || money <= 0 ? true : false,
      });
      this.setData({
        money,
      });
    },
  
    // 会员卡储值接口
    _getPayment(m) {
      let that = this;
      app.util.request({
        url: 'wxapp/wechatCard/payment',
        cachetime: 0,
        data: {
          m: that.data.moduleName,
          openid: that.data.openid,
          money: m,
        },
        success: function(res) {
          if (Number(res.data.errcode) === 0) {
            that.setData({
              payment: res.data.result,
            });
            if (Number(res.data.result.payment.errno) === -1) {
              wx.showToast({
                title: res.data.result.payment.message,
                icon: 'none',
                duration: 1500,
                mask: true,
              });
              return;
            }
            wx.requestPayment({
              'timeStamp': res.data.result.payment.timeStamp,
              'nonceStr': res.data.result.payment.nonceStr,
              'package': res.data.result.payment.package,
              'signType': res.data.result.payment.signType,
              'paySign': res.data.result.payment.paySign,
              'success'(result) {
                // 触发余额变更
                that._rechargeOperate(res, m);
              },
              'complete'(res) {
                if (res.errMsg === 'requestPayment:fail cancel') {
                  wx.showToast({
                    title: '您取消了支付!',
                    icon: 'none',
                    duration: 2000,
                    mask: true,
                  });
                }
              },
            });
          } else {
            wx.showToast({
              title: '调用支付返回失败',
              icon: 'none',
              duration: 2500,
              mask: true,
            });
          }
        },
        fail: function(res) {
          console.log(res);
        },
      });
    },
    
    // 余额变更接口
    _rechargeOperate(response, m) {
      let that = this;
      let { code } = that.data.codeInfo;
      app.util.request({
        url: 'wxapp/wechatCard/recharge',
        cachetime: 0,
        data: {
          code: code,
          logid: response.data.result.logid,
          openid: that.data.openid,
          money: m,
        },
        success: function(res) {
          if (Number(res.data.errcode) === 0) {
            wx.showModal({
              title: '提示',
              content: '充值成功！',
              showCancel: false,
              cancelColor: '#000000',
              confirmText: '确定',
              confirmColor: '#3CC51F',
              success: res => {
                if (res.confirm) {
                  that._checkCard();
                 }
              },
            });
          } else {
            console.log(res);
          }
        },
        fail: function(res) {
          console.log(res);
        },
      });
    },
  
    // 获取充值活动
    _rechargeActivity() {
      let that = this;
      app.util.request({
        url: 'wxapp/wechatCard/rechargeActivity',
        cachetime: 0,
        data: {
          m: that.data.moduleName,
          openid: that.data.openid,
        },
        success: function(res) {
          that.setData({
            activityInfo: res.data.result,
          });
        },
      });
    },
  
    _checkCard() {
      let that = this;
      let unionid = wx.getStorageSync('user_info').unionid;
      let objdata = {
          m: that.data.moduleName,
          openid: that.data.openid,
        };
      if (unionid) objdata.unionid = unionid;

      app.util.request({
        url: 'wxapp/wechatCard/checkCard',
        cachetime: 0,
        data: objdata,
        success: function(res) {
          that.setData({ codeInfo: res.data.result ? res.data.result : '' });
        },
      });
    },
  
    // 获取二维码地址
    _qrcode() {
      let that = this;
      app.util.request({
        url: 'wxapp/wechatCard/qrcode',
        data: {
          openid: that.data.openid,
        },
        cachetime: 0,
        success: function(res) {
          that.setData({
            qrcode: res.data.result.qrcode_url,
          });
        },
      });
    },
  },

  attached() {
    let that = this;
    that.setData({
      user: that.data.user,
    });
  },

  ready() {
      this._checkCard();
      this._qrcode();
      this._rechargeActivity();
  },
});